package com.itheima.service.impl;

import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if(orderSettings != null && orderSettings.size() > 0){
            for (OrderSetting orderSetting : orderSettings) {
                Date date = orderSetting.getOrderDate();
                long count = orderSettingDao.findCountByOrderDate(date);
                System.out.println(count);
                if(count > 0){
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        HashMap<String, String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(map);
        ArrayList<Map> maps = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            HashMap m = new HashMap();
            m.put("date",orderSetting.getOrderDate().getDate());
            m.put("number",orderSetting.getNumber());
            m.put("reservations",orderSetting.getReservations());
            maps.add(m);
        }
        return maps;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date date = orderSetting.getOrderDate();
        long count = orderSettingDao.findCountByOrderDate(date);
        System.out.println(count);
        if(count > 0){
            System.out.println(orderSetting);
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            orderSettingDao.add(orderSetting);
        }
    }
}
